package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.util.Log;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.CodeInt;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.DataManager;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IHardwareAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.AwaitDialog;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 作者：Administrator on 2017/6/28 23:23
 * 邮箱：zhanghuaiha@gmail.com
 */

public class HardwarePresenter extends BasePresenter<IHardwareAc> {

    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;
    private AwaitDialog selfDialog;

    public HardwarePresenter(BaseActivity context) {
        super(context);
    }

    public void loadContacts(){
        manager = new DataManager(mContext);
        mCompositeSubscription = new CompositeSubscription();
    }

    public void unLoadData(){
        if (mCompositeSubscription.hasSubscriptions()){
            mCompositeSubscription.unsubscribe();
        }
    }

    public void loadData(String alias,String udid,String content) {
        Dialog("正在提交..请等待/");
        String time = System.currentTimeMillis() + "";
        String eid = App.SP.getString("id", "");
        String versionName = ObtainVersion.getVersionName(mContext);
        String encry = ObtainEncry.getHardware("2", time, versionName, NetWorkCodeInfo.HARDWARE_FAULT, udid, alias, eid,content);
        mCompositeSubscription.add(manager.getHardware("2",time,versionName, NetWorkCodeInfo.HARDWARE_FAULT,udid,alias,eid ,content,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponseEntity>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        selfDialog.dismiss();
                    }

                    @Override
                    public void onNext(BaseResponseEntity codeInt) {
                        Log.e("HardwarePresenter",codeInt.code+"");
                        if (codeInt.code == 200) {
                            ToastUtil.showToast("提交成功...");
                            mContext.finish();
                        }else {
                            ToastUtil.showToast(codeInt.message);
                        }
                        selfDialog.dismiss();
                    }
                }));
    }

    public void Dialog(String a) {
        selfDialog = new AwaitDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage(a);
        selfDialog.show();
    }
}
